home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Graphic Elements 3 / GEDemo / Walk.c < prev    next >
Text File  |  1995-08-25  |  4KB  |  124 lines

  1. /*
  2.     Walk.c
  3.     
  4.     Animated walking figure on balcony
  5.     
  6.     Copyright 1993 by Al Evans. All rights reserved.
  7.     
  8.     11/8/93
  9.     
  10. */
  11.  
  12. #include "Walk.h"
  13. #include "Motion.h"
  14. #include "SFXCtrlr.h"
  15. #include "SFXProcs.h"
  16. #include "GESound.h"
  17.  
  18.  
  19.  
  20. pascal void DisposeWalker(GEWorldPtr world, GrafElPtr walker)
  21. {
  22.     if (walker->drawData)
  23.         DisposPtr(walker->drawData);
  24.     if (walker->changeData)
  25.         DisposPtr(walker->changeData);
  26. }
  27.  
  28.  
  29. Boolean LoadBalconyScene(GEWorldPtr world)
  30. {
  31.     GrafElPtr        thisElement;
  32.     Rect            balconyBox;            //Walking figure positioned relative to balcony
  33.     short            elemHeight;
  34.     MParamPtr        walkMotion;
  35.     
  36.     //Get railing of balcony
  37.     thisElement = NewBasicPICT(world, balconyID, balconyPlane, rBalconyPic,
  38.                                 transparent, balconyLeft, balconyTop);
  39.     if (thisElement == nil) return false;
  40.     balconyBox = thisElement->animationRect;
  41.     
  42.     //Get walking figure
  43.     thisElement = NewAnimatedGraphic(world, walkID, walkPlane, rAnimWalk,
  44.                                 transparent, 0, 0, 10);
  45.     if (thisElement == nil) return false;
  46.  
  47.     //Draw walker masked
  48.     thisElement->drawData = MakeMask(&((GrafPtr) thisElement->graphWorld)->portBits, 0);
  49.     thisElement->copyMode = srcCopy;
  50.     
  51.     //Be sure to dispose mask & motion
  52.     SetCleanupProc(world, walkID, DisposeWalker);
  53.     
  54.  
  55.     //Position figure relative to balcony
  56.     elemHeight = thisElement->graphRect.bottom - thisElement->graphRect.top;
  57.     PtrMoveElementTo(world, thisElement, balconyBox.left + ScaleToWorld(world, 20),
  58.                              balconyBox.bottom - ScaleToWorld(world, 10) - elemHeight, false);
  59.     
  60.     //Initialize motion fields -- never collide with top or bottom
  61.     walkMotion = (MParamPtr) NewPtrClear(sizeof(MotionParams));
  62.     InitMotion(walkMotion, 100, 100);
  63.     walkMotion->currMotion.h = 6;
  64.     walkMotion->limitRect.top  = 0;
  65.     walkMotion->limitRect.left = balconyBox.left + ScaleToWorld(world, 5);
  66.     walkMotion->limitRect.bottom = 1000;
  67.     walkMotion->limitRect.right = balconyBox.right - ScaleToWorld(world, 16);
  68.     
  69.     //Initialize figure's walking action
  70.     SetAutoChange(world, walkID, DoWalker, (Ptr) walkMotion, 133);
  71.     
  72.     //And set animation style to "loop"
  73.     ((SeqGraphicPtr) thisElement)->seq = loop;
  74.     
  75.     //Load speed control slider
  76.     thisElement = NewSliderSensor(world, sliderID, sliderPlane, rSliderBkg, 
  77.                     sliderLeft, sliderTop, hSlideSensor, rSliderCtrl);
  78.     if (thisElement == nil) return false;
  79.     SetSliderPercent(world, sliderID, 50);
  80.     SetSensorAction(world, sliderID, AdjustSpeed);
  81.     (void) DoGESFX(world, 'SFX1', thisElement, SFXHWipe, 30, 3000, 40, true, true);
  82.     
  83.     return true;
  84. }
  85.  
  86.  
  87. pascal void DoWalker(GEWorldPtr world, GrafElPtr walker)
  88. {
  89.     MParamPtr    motion;
  90.     GEDirection    collisionDir;
  91.     
  92.     motion = (MParamPtr) walker->changeData;
  93.     collisionDir = CheckLimits(&walker->animationRect, &motion->limitRect);
  94.     if ((collisionDir == left) || (collisionDir == right)) {
  95.         motion->currMotion.h = -motion->currMotion.h;
  96.         SetMirroring(world, walker->objectID, (collisionDir == right), false);
  97.     }
  98.     MoveElement(world, walker->objectID, motion->currMotion.h, motion->currMotion.v);
  99.     BumpFrame(world, walker->objectID);
  100.     if (world->userData != nil)  {    // if we have a sound record
  101.         if ((((SeqGraphicPtr) walker)->currentFrame == 3) || 
  102.             (((SeqGraphicPtr) walker)->currentFrame == 8))
  103.                 GEScheduleSound((GESoundPtr) world->userData, rWalkSnd, 1, 0);
  104.     }
  105.         
  106. }
  107.  
  108. pascal void AdjustSpeed(GEWorldPtr world, GrafElPtr ignore, short newSpeed)
  109. {
  110.     GrafElPtr walker;
  111.     long newIntrvl;
  112.     
  113.     walker = FindElementByID(world, walkID);
  114.     if (walker) {
  115.         newIntrvl = (newSpeed * 240) / 100;
  116.         if (newIntrvl > 0) {
  117.             newIntrvl = 240 - newIntrvl;
  118.             if (newIntrvl < 16)
  119.                 newIntrvl = 16;
  120.         }
  121.         walker->changeIntrvl = newIntrvl;
  122.     }
  123. }
  124.